<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelFees extends JModel
{
	/**
	 * fees data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * Method to get fees data
	 *
	 * @access public
	 * @return array
	 */
	function getData() {

		// Lets load the fees if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObjectList();
		}

		return $this->_data;
	}

	/**
	 * Method to build the query for the fees
	 *
	 * @access private
	 * @return integer
	 * @since 1.0
	*/
	function _buildQuery()
	{
		$user =& JFactory::getUser();

		// Get the WHERE and ORDER BY clauses for the query
		$orderby	= $this->_buildContentOrderBy();
		$where		= $this->_buildContentWhere();

		//Query to retrieve all fees that belong under the web links section and that are published.
		$query = ' SELECT * FROM #__ownservice_fees '
				.$where
				.' GROUP BY id '
				.$orderby;

		return $query;
	}

	/**
	 * Method to build the orderby clause of the query for the fees
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentOrderBy(){
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest($option.'.fee.filter_order', 'filter_order', 'fee', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.fee.filter_order_Dir', 'filter_order_Dir', '', 'cmd');

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;

		return $orderby;
	}
	
	/**
	 * Method to build the where clause of the query for the fee
	 *
	 * @access private
	 * @return string
	 * @since 1.0
	 */
	function _buildContentWhere()
	{
		global $mainframe, $option;

		$user = & JFactory::getUser();
		
		//get var
		$rate_id	= JRequest::getVar('rate_id', 0);
		$type		= JRequest::getVar('type', '');
		
		$where = array();
		
		$where[] = 'rate_id='.$rate_id;
		$where[] = 'type='.$this->_db->Quote($type);
		
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		return $where;

	}

	/**
	 * Method to remove a fees
	 *
	 * @access public
	 * @return boolean true on success
	*/
	function delete($id){

		$user = & JFactory::getUser();

		$query = " DELETE FROM #__ownservice_fees WHERE id=".$id;

		$this->_db->setQuery($query);
		if(!$this->_db->query()){
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return true;
	}
	
	/**
	 * Method to get Exchange Rate
	 * 
	 * @access public
	 * @return object
	 * @since 1.5
	 *
	 */
	 function getExchangeRate($id)
	 {
	 	$query = 'SELECT c.* '
	 			.' FROM #__ownservice_currencies AS c '
	 			.' LEFT JOIN #__ownservice_exchangerates AS r ON r.currency_id=c.id'
				.' WHERE r.id='.$id;
				
		$this->_db->setQuery($query);
		$row = $this->_db->loadObject();
	 	return $row;
	 }
}